package com.liujiahe.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.liujiahe.entity.City;
import com.liujiahe.mapper.CityMapper;
import com.liujiahe.service.ICityService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author 刘家贺
 * @since 2022-06-30
 */
@Service
public class CityServiceImpl extends ServiceImpl<CityMapper, City> implements ICityService {

    @Autowired
    CityMapper mapper;

    @Override
    public List<City> tree() {
        // 第一级省
        QueryWrapper<City> wrapper = new QueryWrapper<>();
        wrapper.eq("pid",1);
        List<City> sList = mapper.selectList(wrapper);
        // 第二级市
        for (City city : sList) {
            QueryWrapper<City> wrapper1 = new QueryWrapper<>();
            wrapper1.eq("pid",city.getId());
            List<City> yList = mapper.selectList(wrapper1);
            city.setDis(yList);
            // 第三级县
            yList.stream().forEach(xian -> {
                QueryWrapper<City> wrapper2 = new QueryWrapper<>();
                wrapper2.eq("pid",xian.getId());
                List<City> xList = mapper.selectList(wrapper2);
                xian.setDis(xList);
            });
        }
        return sList;
    }

}
